﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using NExt.Handler.Base.Common.Manager;

namespace NExt.Handler.Component.Default.JsSerialize
{
    public class ConstructorScriptGenerator : AbstractNExtScriptGenerator
    {
        private const string SCRIPT_POSTFIX = "Constructor.js";
        private const string CONSTRUCTOR_SCRIPT_FORMAT = "{0} = function(config) {{\r\n{1}\r\n}};";

        public ConstructorScriptGenerator()
            : base(SCRIPT_POSTFIX)
        {
        }

        protected override string BuildDefaultContent(Type type)
        {
            string constructorBody = BuildConstructorBody(type);
            if (string.IsNullOrEmpty(constructorBody))
                return string.Empty;

            string fullName = type.FullName;
            string content = string.Format(CONSTRUCTOR_SCRIPT_FORMAT, fullName, constructorBody);

            return content;
        }

        private static string BuildConstructorBody(Type type)
        {
            string fullName = type.FullName;

            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat("    {0}.superclass.constructor.call(this, config);", fullName);

            string content = stringBuilder.ToString();
            return content;
        }
    }
}
